package com.zhu.shopcenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhu.commons.utils.OrderNumber;
import com.zhu.service.base.handler.error.LoginErrorException;
import com.zhu.shopcenter.feign.CourseFeign;
import com.zhu.shopcenter.feign.memberFeign;
import com.zhu.shopcenter.pojo.TradeOrder;
import com.zhu.shopcenter.mapper.TradeOrderMapper;
import com.zhu.shopcenter.service.ITradeOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.shopcenter.vo.CourseVo;
import com.zhu.shopcenter.vo.MemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2022-08-19
 */
@Service
public class TradeOrderServiceImpl extends ServiceImpl<TradeOrderMapper, TradeOrder> implements ITradeOrderService {
    @Autowired
    private CourseFeign courseFeign;
    @Autowired
    private memberFeign memberFeign;
    @Autowired
    private TradeOrderMapper orderMapper;
    @Override
    public String saveOrder(String courseId, String id) throws Exception {
        /**
         * 查询当前用户是否已经购买
         *
         */
        LambdaQueryWrapper<TradeOrder> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(TradeOrder::getCourseId,courseId);
        queryWrapper.eq(TradeOrder::getMemberId,id);
        TradeOrder tradeOrder = orderMapper.selectOne(queryWrapper);
        if(tradeOrder!=null){
            return tradeOrder.getId();
        }
        CourseVo course = courseFeign.getCourseInfo(courseId);
        MemberVo member = memberFeign.getMember(id);
        if(member==null||course==null){
            throw new  Exception();
        }
        String orderNumber = OrderNumber.getOrderNumber();
        TradeOrder order = new TradeOrder();
        order.setCourseCover(course.getCover());
        order.setCourseId(courseId);
        order.setCourseTitle(course.getTitle());
        order.setOrderNo(orderNumber);
        order.setTeacherName(course.getTeacherName());
        order.setMemberId(id);
        order.setEmail(member.getEmail());
        order.setNickname(member.getNickname());
        order.setTotalFee(course.getPrice().multiply(new BigDecimal(100))); //金额是元，但是数据库是分所*100
        order.setStatus(0);
        order.setPayType(1);
        orderMapper.insert(order);
        return order.getId();
    }

    @Override
    public TradeOrder getOrderInfo(String orderId, String id) {
        LambdaQueryWrapper<TradeOrder> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(TradeOrder::getId,orderId);
        queryWrapper.eq(TradeOrder::getMemberId,id);
        TradeOrder order = orderMapper.selectOne(queryWrapper);
        return order;
    }

    @Override
    public Boolean buyStatus(String courseId, String id) {
        LambdaQueryWrapper<TradeOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TradeOrder::getCourseId, courseId);
        queryWrapper.eq(TradeOrder::getStatus, 1);
        queryWrapper.eq(TradeOrder::getMemberId, id);
        Integer integer = orderMapper.selectCount(queryWrapper);
        return integer.intValue() > 0;
    }

    /**
     * 获取订单列表
     * @param id
     * @return
     */
    @Override
    public List<TradeOrder> getOrderList(String id) {
        LambdaQueryWrapper<TradeOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(TradeOrder::getGmtCreate);
        queryWrapper.eq(TradeOrder::getMemberId, id);
        List<TradeOrder> orderList = orderMapper.selectList(queryWrapper);
        return orderList;
    }

    /**
     * 删除订单列表
     * @param idList
     * @param id
     * @return
     */
    @Override
    public boolean deleteOrderList(List<String> idList, String id) {
        int i = orderMapper.deleteBatchIds(idList);
        if(i>0){
            return true;
        }
return false;

    }
}
